package org.springblade.modules.admin.utils;

import org.springblade.core.tool.utils.Func;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @Date 10:45 2023/10/26
 * @Description {
 *   图片工具类
 * }
 * @Author lsay
 **/
public class WatermarkImgUtils {

	/**
	 * 匹配图片地址正则表达式
	 */
	private static final String REGEX = "(http|https)://[\\w\\d./?&=#+-]+\\.(jpg|jpeg|gif|png)";

	/**
	 * 匹配图片视频地址正则表达式
	 */
	private static final String REGEX_I_V = "(http|https)://[\\w\\d./?&=#+-]+\\.(jpg|jpeg|gif|png|mp4)";

	/**
	 * @Author lsay
	 * @Description {
	 *   根据字符串检索出图片视频地址
	 * }
	 * @Date 11:00 2023/10/26
	 * @Param 目标字符串
	 * @return 图片地址集合
	 **/
	public static List<String> findImgVideoUrl(String text){
		List<String> urls = new ArrayList<>();
		Pattern pattern = Pattern.compile(REGEX_I_V);
		Matcher matcher = pattern.matcher(text);
		while (matcher.find()){
			String url =  matcher.group();
			urls.add(url);
		}
		return urls;
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   根据字符串检索出图片地址
	 * }
	 * @Date 11:00 2023/10/26
	 * @Param 目标字符串
	 * @return 图片地址集合
	 **/
	public static List<String> findImgUrl(String text){
		List<String> urls = new ArrayList<>();
		Pattern pattern = Pattern.compile(REGEX);
		Matcher matcher = pattern.matcher(text);
		while (matcher.find()){
			String url =  matcher.group();
			urls.add(url);
		}
		return urls;
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   根据字符串检索出图片名称
	 * }
	 * @Date 11:01 2023/10/26
	 * @Param 目标字符串
	 * @return 图片名称集合
	 **/
	public static List<String> findImgName(String text){
		List<String> names = new ArrayList<>();
		List<String> urls = findImgUrl(text);
		if(Func.isNotEmpty(urls)){
			names = urls.stream().map(e -> e.substring(e.lastIndexOf("/") + 1)).collect(Collectors.toList());
		}
		return names;
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   根据字符串检索出图片名称（无后缀）
	 * }
	 * @Date 11:01 2023/10/26
	 * @Param 目标字符串
	 * @return 图片名称集合 （无后缀）
	 **/
	public static List<String> findImgNameNotSuffix(String text){
		List<String> names = new ArrayList<>();
		List<String> urls = findImgUrl(text);
		if(Func.isNotEmpty(urls)){
			names = urls.stream().map(e -> e.substring(e.lastIndexOf("/") + 1,e.lastIndexOf("."))).collect(Collectors.toList());
		}
		return names;
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   根据图片地址获取图片名称（无后缀）
	 * }
	 * @Date 14:38 2023/10/27
	 * @Param 图片地址
	 * @return 图片名称（无后缀）
	 **/
	public static String subImgNameNotSuffix(String url){
		return url.substring(url.lastIndexOf("/") + 1,url.lastIndexOf("."));
	}

	/**
	 * @Author lsay
	 * @Description {
	 *   根据图片地址获取图片名称
	 * }
	 * @Date 14:38 2023/10/27
	 * @Param 图片地址
	 * @return 图片名称
	 **/
	public static String subImgName(String url){
		return url.substring(url.lastIndexOf("/"));
	}

	public static void main(String[] args) {
		String text = "{\"max\": 6, \"min\": 4, \"val\": [{\"url\": \"https://592800-erised-fresh.oss-cn-shenzhen.aliyuncs.com/upload/20230803/e53987d89258509bdb44954ca74f606d.jpg\", \"type\": \"image\"}, {\"url\": \"https://592800-erised-fresh.oss-cn-shenzhen.aliyuncs.com/upload/20230803/65a07bef9a4497ec970cff9039972f0e.jpg\", \"type\": \"image\"}, {\"url\": \"https://592800-erised-fresh.oss-cn-shenzhen.aliyuncs.com/upload/20230803/555c43016fa5ba1dde4fde6e440a72f6.jpg\", \"type\": \"image\"}], \"must\": false, \"type\": 3, \"options\": {\"files\": {\"val\": \"\", \"must\": false, \"name\": \"\", \"need\": false, \"placeholder\": \"\"}, \"result\": true, \"records\": {\"val\": \"\", \"must\": false, \"name\": \"\", \"need\": false, \"unit\": \"\", \"placeholder\": \"\"}, \"remarks\": {\"val\": \"\", \"must\": false, \"name\": \"\", \"need\": true, \"placeholder\": \"\"}, \"onlyAlbum\": false}}";
		System.err.println(findImgNameNotSuffix(text));
	}

}
